<?php

namespace app\index\controller;

use think\Controller;
use think\captcha\Captcha;
use Onbuka;

class Passport extends Controller {
	public function initialize() {
		$lang = $this->request->param ( 'lang' );
		$lang ? cookie ( 'think_var', $lang ) : '';
		if ($lang != '') {
			$url = GetCurrentHost () . $_SERVER ['QUERY_STRING'];
			$url = str_replace ( 'lang', 'jumplang', $url );
			$url = str_replace ( 's=/', '', $url );
			$this->redirect ( $url );
		}
		
		$website = model ( 'Website' )->getAll ();
		if ($website ['status'] == 0)
			exit ();
		$this->assign ( 'website', $website );
	}
	function getcode() {
		$type = $this->request->param ( 'type' );
		$account = $type == 'email' ? $this->request->param ( 'email' ) : $this->request->param ( 'phone_mob' );
		if ($account == '')
			echo json_encode ( array ('code' => 98,'msg' => lang ( 'account_not_null' ) ) );
		else {
			$code = model ( 'Verifycode' )->getCode ( array ('type' => $type,'account' => $account ) );
			$content = lang ( 'sms_code_tips' ) . $code;
			if ($code != '') {
				switch ($type) {
					case 'email' :
						$title = lang ( 'verify_code' );
						$log = model ( 'Phpmail' )->sendMail ( $account, $title, $content );
						if ($log == 'success')
							echo json_encode ( array ('code' => 99,'msg' => lang ( 'send_success' ) ) );
						else
							echo json_encode ( array ('code' => 1,'msg' => lang ( 'send_fail' ) ) );
						break;
					case 'mobile' :
						$onbuka = new Onbuka ();
						$onbuka->apiKey = '1FlNcw9R';
						$onbuka->apiSecret = 'V62X1xjg';
						$onbuka->appId = 'bZ3gBdHf';
						$sign = $onbuka->sign ();
						$dataArr ['appId'] = $onbuka->appId;
						$dataArr ['numbers'] = $this->request->param ( 'country' ) . $account;
						$dataArr ['content'] = $content;
						$dataArr ['senderId'] = '';
						$data = json_encode ( $dataArr );
						$result = $onbuka->curl_post ( 'https://api.onbuka.com/v3/sendSms', $data, $sign );
						$result = json_decode ( $result, true );
						if ($result ['status'] == 0)
							echo json_encode ( array ('code' => 99,'msg' => lang ( 'send_success' ) ) );
						else
							echo json_encode ( array ('code' => 1,'msg' => lang ( 'send_fail' ) ) );
						break;
				}
			} else
				echo json_encode ( array ('code' => 98,'msg' => lang ( 'send_fail' ) ) );
		}
	}
	function login() {
		if ($this->request->isPost ()) {
			$captcha = $this->request->param ( 'captcha' );
		if (! captcha_check ( $captcha ))
			echo json_encode ( array ('code' => 0,'msg' => lang ( 'captcha_error' ) ) );
			else {
			//	$map [] = [ 'email','=',$this->request->param ( 'phone_mob' ) ];
			//	$info = model ( 'Member' )->getInfo ( $map );
			//	$num = count ( $info );
			//	if ($num == 0) {
			//		unset($map);
					$map [] = [ 'phone_mob','=',$this->request->param ( 'phone_mob' ) ];
					$info = model ( 'Member' )->getInfo ( $map );
					$num += count ( $info );
					if (count ( $info ) == 0) {
						unset($map);
						$map [] = [ 'user_name','=',$this->request->param ( 'phone_mob' ) ];
						$info = model ( 'Member' )->getInfo ( $map );
						$num += count ( $info );
					}
			//	}
				if ($num == 0)
					echo json_encode ( array ('code' => 1,'msg' => lang ( 'account_not_exist' ) ) );
				else {
					$pwd = $this->request->param ( 'pwd' );
					// $map [] = [ 'password','=',md5 ( $pwd ) ];
					$map [] = [ 'password','=',$pwd ];
					$info = model ( 'Member' )->getInfo ( $map );
					if (count ( $info ) > 0) {
						$data ['last_ip'] = get_client_ip ();
						$data ['logins'] = $info ['logins'] + 1;
						$data ['last_login'] = time ();
						model ( 'Member' )->edit ( array ([ 'user_id','=',$info ['user_id'] ] ), $data );
						session ( 'uid', $info ['user_id'] );
						echo json_encode ( array ('code' => 99,'msg' => lang ( 'login_success' ),'url' => url ( 'Index/index' ) ) );
					} else
						echo json_encode ( array ('code' => 2,'msg' => lang ( 'login_error' ) ) );
				}
			}
		} else
			return $this->fetch ();
	}
	function signup() {
		if ($this->request->isPost ()) {
			$type = $this->request->param ( 'type' );
			$phone_mob = $this->request->param ( 'phone_mob' );
			$email = $this->request->param ( 'email' );
			$username = $this->request->param ( 'username' );
			$pwd1 = $this->request->param ( 'pwd1' );
			$pwd2 = $this->request->param ( 'pwd2' );
			$paypwd1 = $this->request->param ( 'paypwd1' );
			$paypwd2 = $this->request->param ( 'paypwd2' );
			$captcha = $this->request->param ( 'captcha' );
			$code = $this->request->param ( 'code' );
			if (! captcha_check ( $captcha ))
			    echo json_encode ( array ('code' => 0,'msg' => lang ( 'captcha_error' ) ) );
			else {
			if ($pwd1 == '' || $pwd1 != $pwd2)
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'password_error' ) ) ) );
			elseif ($paypwd1 == '' || $paypwd1 != $paypwd2)
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'paypwd_error' ) ) ) );
			elseif ($code == '')
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );
			$info = model ( 'Member' )->getInfo ( array ([ 'user_name','=',$username ] ) );
			if (count ( $info ) > 0)
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'username_exist' ) ) ) );
			
			// if ($type == 'email')
		//$codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$email ],[ 'type','=',$type ],[ 'code','=',$code ] ) );
			// else
			// $codeInfo = model ( 'Verifycode' )->getInfo ( array ([ 'account','=',$phone_mob ],[ 'type','=',$type ],[ 'code','=',$code ] ) );
			//if (count ( $codeInfo ) == 0)
				//die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );
			// if ($type == 'email') {
			$info = model ( 'Member' )->getInfo ( array ([ 'email','=',$email ] ) );
			if (count ( $info ) > 0)
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'email_exist' ) ) ) );
			// } else {
			$info = model ( 'Member' )->getInfo ( array ([ 'phone_mob','=',$phone_mob ] ) );
			if (count ( $info ) > 0)
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'phone_exist' ) ) ) );
			// }
			
			$data ['user_name'] = $username;
			$data ['email'] = $email;
			// $data ['password'] = md5 ( $pwd1 );
			$data ['password'] = $pwd1;
			$data ['phone_mob'] = $phone_mob;
			$data ['reg_time'] = time ();
			$data ['last_ip'] = get_client_ip ();
			$data ['logins'] = 0;
			$data ['status'] = 1;
			$data ['role_id'] = 0;
			$data ['country'] = $this->request->param ( 'country' );
			$data ['money'] = 0;
			$data ['wallet_type'] = $this->request->param ( 'wallet_type' );
			$data ['usdt_wallet'] = $this->request->param ( 'usdt_wallet' );
			$data ['paypwd'] = $paypwd1;
			$log = model ( 'Member' )->add ( $data );
			if ($log > 0) {
				session ( 'uid', $log );
				echo json_encode ( array ('code' => 99,'msg' => lang ( 'register_success' ),'url' => url ( 'Index/index' ) ) );
			} else
				echo json_encode ( array ('code' => 2,'msg' => lang ( 'register_error' ) ) );
		   }
		} else
			return $this->fetch ();
	}
	function forgot() {
		if ($this->request->isPost ()) {
			$verify = $this->request->param ( 'captcha' );
			$username = $this->request->param ( 'username' );
			$paypwd = $this->request->param ( 'paypwd' );
			$map [] = [ 'user_name','=',$username ];
			$map [] = [ 'paypwd','=',$paypwd ];
			$info = model ( 'Member' )->getInfo ( $map );
			if (! captcha_check ( $verify ))
				die ( json_encode ( array ('code' => 1,'msg' => lang ( 'verifycode_error' ) ) ) );
			elseif (count ( $info ) > 0) {
				$pwd1 = $this->request->param ( 'pwd1' );
				$pwd2 = $this->request->param ( 'pwd2' );
				if ($pwd1 != '' && $pwd1 == $pwd2) {
					// $data ['password'] = md5 ( $pwd1 );
                    $data ['password'] = $pwd1;
                    $log = model ( 'Member' )->edit ( array ([ 'user_id','=', $info['user_id'] ] ), $data );
                    if ($log > 0)
                        echo json_encode ( array ('code' => 99,'msg' => lang ( 'run_success' ) ) );
                    else
                        echo json_encode ( array ('code' => 0,'msg' => lang ( 'run_fail' ) ) );
				} else
					echo json_encode ( array ('code' => 0,'msg' => lang ( 'password_error' ) ) );
			} else
				echo json_encode ( array ('code' => 0,'msg' => lang ( 'paypwd_error' ) ) );
		} else
			return $this->fetch ();
	}
	function online() {
		if (session ( 'order_id' ) > 0) {
			model ( 'Order' )->edit ( array ([ 'order_id','=',session ( 'order_id' ) ] ), array ('online_time' => time () ) );
			echo json_encode ( array ('code' => 99,'id' => session ( 'order_id' ),'online' => 1 ) );
		} else
			echo json_encode ( array ('code' => 0 ) );
	}
	function logout() {
		$_SESSION ['uid'] = '';
		$_SESSION ['uname'] = '';
		$_SESSION ['sign'] = '';
		unset ( $_SESSION );
		session ( null );
		$this->redirect ( url ( 'login' ) );
	}
	public function captcha() {
		$config = [ 'fontSize' => 30,'length' => 4,'useCurve' => false,'useNoise' => false ];
		$captcha = new Captcha ( $config );
		$captcha->codeSet = '0123456789';
		return $captcha->entry ();
	}
		function lang() {
		return $this->fetch ();
	}
}
